package com.yswy.backstage.controller;

import com.yswy.backstage.dto.HwyRefreshCdnDto;
import com.yswy.backstage.dto.UploadImgVo;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/hwy")
@Api(tags = "华为云工具")
public class HwyController {
    @Autowired
    private HuaWeiYunOBSUtil obsUtil;

    /** 获取图片上传链接 **/
    @PostMapping("/getUploadImgUrl")
    @ApiOperation(value = "获取上传链接")
    public ResultModel getUploadImgUrl(@RequestBody() UploadImgVo uploadImgVo) {
        List<String> list = new ArrayList<>();
        if (uploadImgVo.getNames().size() > 0) {
            for (String s:uploadImgVo.getNames()) {
                String suffix = s.substring(s.lastIndexOf(".") + 1);
                String fileNewName = UUID.randomUUID().toString();
                String str = obsUtil.getUploadSignUrl("datePlatform/"+fileNewName+"."+suffix,null);
                list.add(str);
            }
        }
        return ResultModel.ok(list);
    }


    /** 刷新 OBS CDN 缓存 **/
    @PostMapping("/refreshTasks")
    @ApiOperation(value = "刷新 OBS CDN 缓存")
    public void refreshTasks(@RequestBody @Validated HwyRefreshCdnDto filter) {
        HuaWeiYunOBSUtil.refreshTasks(filter.getObsFilePath(), filter.getType());
    }

}
